package com.data.structure.node;

/**
 * @author zflu
 * 构造一个栈
 * Created by lucky on 2018-12-10.
 */
public class ArrayStack {

    private long[] a;

    private int size;

    private int top;

    public ArrayStack(int maxSize){
        this.size = maxSize;
        this.a = new long[size];
        this.top = -1;
    }

    /**
     * 存栈
     * @param v
     */
    public void push(long v){
        if(isFull()){
            System.out.println("栈已经满了。。。");
            return;
        }
        a[++top] = v;
    }

    /**
     * 取栈
     * @return
     */
    public long peek(){
        if(isEmpty()){
            System.out.println("栈现在为空!");
            return 0;
        }
        return a[top];
    }

    /**
     * 弹栈
     * @return
     */
    public long pop(){
        if(isEmpty()){
            System.out.println("栈现在为空!");
            return 0;
        }
        return a[top--];
    }

    /**
     * 获取长度
     * @return
     */
    public int size(){
        return top + 1;
    }

    /**
     * 判断栈是否已经满
     * @return
     */
    public boolean isFull(){
        return (size -1 == top);
    }

    /**
     * 判断栈是否为空
     * @return
     */
    public boolean isEmpty(){
        return (top == -1);
    }

    public static void main(String[] args) {
        ArrayStack stack = new ArrayStack(10);
        stack.push(1);
        stack.push(2);
        stack.push(3);
        System.out.println(stack.peek());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.size());
    }
}
